package indi.zhifa.study2024.class002.busy.generalTest.business.curd.inv.storage.service.impl;

import indi.zhifa.study2024.class002.busy.generalTest.business.curd.define.item.dao.service.IItemDefineDbService;
import indi.zhifa.study2024.class002.busy.generalTest.business.curd.define.item.entity.po.ItemDefineEntity;
import indi.zhifa.study2024.class002.busy.generalTest.business.curd.inv.storage.dao.service.IItemInventoryDbService;
import indi.zhifa.study2024.class002.busy.generalTest.business.curd.inv.storage.entity.po.ItemInventoryEntity;
import indi.zhifa.study2024.class002.busy.generalTest.business.curd.inv.storage.entity.request.InvInRequest;
import indi.zhifa.study2024.class002.busy.generalTest.business.curd.inv.storage.entity.response.InvInResponse;
import indi.zhifa.study2024.class002.busy.generalTest.business.curd.inv.storage.service.IItemInventoryService;
import indi.zhifa.study2024.core.common.util.DtoEntityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;

@RequiredArgsConstructor
@Service
public class ItemInventoryServiceImpl implements IItemInventoryService {

    private final IItemDefineDbService mItemDefineDbService;
    private final IItemInventoryDbService mItemInventoryDbService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public InvInResponse stockGood(Long pId, InvInRequest pInvInRequest) {

        ItemDefineEntity itemDefineEntity = mItemDefineDbService.check(pId);
        ItemInventoryEntity itemInventoryEntity = mItemInventoryDbService.findByItemId(itemDefineEntity.getId());
        Long storage = 0l;
        switch (itemDefineEntity.getUnitType()) {
            case SINGLE_UNIT -> {
                storage = pInvInRequest.getSmallUnitCnt();
            }
            case NORMAL_UNIT -> {
                storage = pInvInRequest.getSmallUnitCnt() + pInvInRequest.getLargeUnitCnt() * itemDefineEntity.getLargeConvert();
            }
            case TRIPLE_UNIT -> {
                storage = pInvInRequest.getSmallUnitCnt()
                        + pInvInRequest.getMidUnitCnt() * itemDefineEntity.getMidConvert()
                        + pInvInRequest.getLargeUnitCnt() * itemDefineEntity.getLargeConvert();
            }
        }
        Long totalCost = pInvInRequest.getTotalCost();
        BigDecimal totalCostBigDecimal;
        if(null ==  totalCost|| 0 == totalCost) {
            totalCostBigDecimal = BigDecimal.valueOf(storage).multiply(pInvInRequest.getPriceLarge())
                    .divide(BigDecimal.valueOf(itemDefineEntity.getLargeConvert()),4,RoundingMode.HALF_UP);
        }else{
            totalCostBigDecimal = new BigDecimal(totalCost).divide(BigDecimal.valueOf(100),4, RoundingMode.HALF_UP);
        }

        Long newStorage = itemInventoryEntity.getStorage() + storage;
        BigDecimal newPrice = itemInventoryEntity.getCurPrice()
                .multiply(BigDecimal.valueOf(itemInventoryEntity.getStorage()))
                .add(totalCostBigDecimal)
                .divide(BigDecimal.valueOf(itemInventoryEntity.getStorage() + storage),5, RoundingMode.HALF_UP);
        itemInventoryEntity.setStorage(newStorage);
        itemInventoryEntity.setCurPriceLarge(newPrice.multiply(BigDecimal.valueOf(itemDefineEntity.getLargeConvert())).setScale(2,RoundingMode.HALF_UP));
        itemInventoryEntity.setCurPrice(newPrice.setScale(4,RoundingMode.HALF_UP));
        mItemInventoryDbService.updateById(itemInventoryEntity);
        InvInResponse invInResponse = DtoEntityUtil.trans(itemInventoryEntity, InvInResponse.class);
        return invInResponse;
    }
}
